package com.smec.remes.module.scan.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.smec.moblie.R;
import com.smec.moblie.databinding.ItemOutMaterBinding;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.module.scan.beans.SmecMateriel;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by xupeizuo on 2018/3/28.
 */

public class SmecOutAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private Context mContext;
    private ArrayList<SmecMateriel> arrayList=new ArrayList<>();
    private boolean isChoose=false;
    public static final int VIEW_DATA=10002;
    public static final int VIEW_NULL=10001;

    public SmecOutAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == VIEW_DATA){
            ItemOutMaterBinding outMaterBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_out_mater,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(outMaterBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(outMaterBinding);
            return smecBaseRecyclerViewHolder;
        }
        if(viewType == VIEW_NULL){
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final SmecBaseRecyclerViewHolder holder, final int position) {
        if(holder.getViewType() == VIEW_DATA){
            ItemOutMaterBinding outMaterBinding=(ItemOutMaterBinding)holder.getBinding();
            outMaterBinding.setOutItem(arrayList.get(position));
            outMaterBinding.setIsChoose(isChoose);
            outMaterBinding.ivCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    arrayList.get(position).setChoose(!arrayList.get(position).isChoose());
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(arrayList == null || arrayList.size() == 0){
            return VIEW_NULL;
        }else {
            return VIEW_DATA;
        }
    }

    public void setArrayList(ArrayList<SmecMateriel> arrayList) {
        if(CommonUtils.notEmpty(arrayList)){
            this.arrayList = arrayList;
        }else {
            this.arrayList=new ArrayList<>();
        }
        notifyDataSetChanged();
    }

    public void setChoose(boolean choose) {
        isChoose = choose;
        notifyDataSetChanged();
    }

    /**
     * 获取选中的物料
     * @return
     */
    public ArrayList<SmecMateriel> getMakeSure(){
        if(!isChoose){//如果没有点击选择怎默认全选
            ArrayList<SmecMateriel> list=new ArrayList<>();
            if(CommonUtils.notEmpty(arrayList)){
                for(int i=arrayList.size()-1;i>=0;i--){
                    SmecMateriel smecMateriel=arrayList.get(i);
                    if(smecMateriel.getUseQty()!=0){
                        list.add(smecMateriel);
                    }
                }
            }
            return list;
        }else {
            ArrayList<SmecMateriel> materiels=new ArrayList<>();
            if(CommonUtils.notEmpty(arrayList)){
                for(int i=arrayList.size()-1;i>=0;i--){
                    SmecMateriel smecMateriel=arrayList.get(i);
                    if(smecMateriel.isChoose()){
                        materiels.add(smecMateriel);
                    }
                }
            }
            return materiels;
        }
    }

    /**
     * 删除选中的物料
     */
    public void removeChoose(){
        if(CommonUtils.notEmpty(arrayList)){
            for(int i=arrayList.size()-1;i>=0;i--){
                SmecMateriel smecMateriel=arrayList.get(i);
                if(smecMateriel.isChoose()){
                    arrayList.remove(i);
                }
            }
            notifyDataSetChanged();
        }
    }

    public ArrayList<SmecMateriel> getArrayList() {
        return arrayList;
    }

    public boolean isChoose() {
        return isChoose;
    }
}
